package com.bears.god.sup.servlet.operation;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.bears.god.sup.model.Dept;
import com.bears.god.sup.model.Income;
import com.bears.god.sup.service.IncomeService;
import com.bears.god.sup.service.ServiceDAOFactory;

@WebServlet("/addIncome.do")
public class AddIncomeServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 000004L;

	public static final String METHOD_DEPT = "0";
	public static final String METHOD_FINISH = "1";
	public static final String METHOD_ADD = "2";

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setCharacterEncoding("UTF-8");
		req.setCharacterEncoding("UTF-8");
		String method = req.getParameter("method");

		if (method != null) {
			switch (method) {
			case AddIncomeServlet.METHOD_DEPT:
				dept(req, resp);
				break;
			case AddIncomeServlet.METHOD_FINISH:
				finish(req, resp);
				break;
			case AddIncomeServlet.METHOD_ADD:
				add(req, resp);
				break;
			}

		}
	}

	private void add(HttpServletRequest req, HttpServletResponse resp) {
		try {
			req.getRequestDispatcher("WEB-INF/view/manager/addincome.jsp").forward(req, resp);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void finish(HttpServletRequest req, HttpServletResponse resp) {
		Income income = new Income();

		try {
			String deptName = req.getParameter("deptName");
			String daily = req.getParameter("daily");
			String bussinessDate = req.getParameter("bussiness_date");
			String lastModDate = req.getParameter("last_mod_date");

			// new String(deptName.getBytes("ISO-8859-15"), "UTF-8");
			income.setDeptName(deptName);

			income.setDailyIncome(Double.parseDouble(daily));
			// income.setDailyIncome(20000);
			income.setBussDate(bussinessDate);
			income.setLastDate(lastModDate);

			IncomeService service = ServiceDAOFactory.getIncomeDAO();
			service.save(income);

			req.setAttribute("AddIncome_err", "添加成功");
			// System.out.println("成功"+deptName);
			req.getRequestDispatcher("/Income").forward(req, resp);
		} catch (Exception e) {
			e.printStackTrace();
			try {
				req.setAttribute("AddIncome_err", "添加失败");
				req.getRequestDispatcher("WEB-INF/view/manager/addincome.jsp").forward(req, resp);
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		}
	}

	private void dept(HttpServletRequest req, HttpServletResponse resp) {
		List<Dept> depts;

		try {
			depts = com.bears.god.sup.service.ServiceDAOFactory.getDeptDAO().findByAll();
			com.alibaba.fastjson.JSONObject json = new com.alibaba.fastjson.JSONObject();
			com.alibaba.fastjson.JSONArray array = new com.alibaba.fastjson.JSONArray();

			for (Dept dept : depts) {
				com.alibaba.fastjson.JSONObject json1 = (com.alibaba.fastjson.JSONObject) JSON.toJSON(dept);
				array.add(json1);
			}
			json.put("depts", array);
			PrintWriter out = resp.getWriter();
			out.print(json.toString());
			out.close();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
